import pages from "@/router/pages.js";
import views from "@/router/views.js";
import {createRouter, createWebHistory} from "vue-router";
import Basic from "@/layout/Basic.vue";
import user from "@/store/user.js";

const routes = [
    ...pages,
    {
        path: '/basic',
        name: 'basic',
        component: Basic,
        redirect: views[0].path,
        children: [
            ...views
        ]
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
const publicPageName = ['demo']
router.beforeEach((to, from, next) => {
    const token = user().token
    if (to.path === '/login') {
        if (token) {
            next('/basic')
        } else {
            next()
        }
    } else {
        if (publicPageName.includes(to.name)) {
            next()
        } else {
            if (token) {
                next()
            } else {
                next('/login')
            }
        }
    }
})
export default router